package org.itboys.mobile.service.mongo.weixin;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.activity.SundayActivityRelatedDto;
import org.itboys.mobile.entity.mongo.SundayActivityRelated;
import org.itboys.mobile.entity.mongo.SundayLouKa;
import org.itboys.mobile.entity.mongo.SundayNews;
import org.itboys.mobile.entity.mysql.SundayProduct;
import org.itboys.mobile.service.mongo.web.SundayLouKaService;
import org.itboys.mobile.service.mongo.web.SundayNewsService;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/7/19
 * @Description：微信小程序活动相关内容
 */
@Service
public class SundayWeiXinActivityRelatedService extends BaseMongoService<SundayActivityRelated> {
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayActivityRelated> getEntityClass() {
        return SundayActivityRelated.class;
    }
    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayLouKaService louKaService;
    @Autowired
    private SundayNewsService newsService;
    @Autowired
    private ResourceHolder resourceHolder;


    /**
     * 根据活动id获取相关内容
     * @return
     */
    public List<SundayActivityRelatedDto> select(Long activityId){
        List<SundayActivityRelatedDto> result=new ArrayList<>();
        Map<String, Object> param = new HashMap<>();
        param.put("activityId",activityId);
        param.put("isDeleted", SundayCommonConstants.NO);
        //查询所有的相关，根据相关的类型分类匹配数据
        List<SundayActivityRelated> data = super.list(param);

        if(ListUtil.isNull(data)){return result;}
        //楼盘id集合
        List<Long> productIds=new ArrayList<>();
        //楼卡id集合
        List<Long> louKaIds=new ArrayList<>();
        //新闻id集合
        List<Long> newsIDs=new ArrayList<>();
        for (SundayActivityRelated related:data) {
            if(SundayCommonConstants.activity_related_product.equals(related.getType())){
                productIds.add(related.getOtherId());
                continue;
            }
            if(SundayCommonConstants.activity_related_louKa.equals(related.getType())){
                louKaIds.add(related.getOtherId());
                continue;
            }
            if(SundayCommonConstants.activity_related_news.equals(related.getType())){
                newsIDs.add(related.getOtherId());
                continue;
            }
        }
        //定义三种相关集合
        List<SundayProduct> products=new ArrayList<>();
        List<SundayLouKa> louKas=new ArrayList<>();
        List<SundayNews> newss=new ArrayList<>();
        //查询所有相关楼盘
        if(ListUtil.isNotNull(productIds)){
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("ids",productIds);
            products = productService.select(param);
        }
        //查询所有相关楼卡
        if(ListUtil.isNotNull(louKaIds)){
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("id in",louKaIds);
            louKas = louKaService.list(param);
        }
        //查询所有相关新闻
        if(ListUtil.isNotNull(newsIDs)){
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("id in",newsIDs);
            newss = newsService.list(param);
        }
        //封装dto集合
        for (SundayActivityRelated related:data) {
            SundayActivityRelatedDto relatedDto=new SundayActivityRelatedDto();
            relatedDto.setId(related.getOtherId());
            if(SundayCommonConstants.activity_related_product.equals(related.getType())){
                for (SundayProduct product:products) {
                    if(related.getOtherId().equals(product.getId())){
                        relatedDto.setType(SundayCommonConstants.activity_related_product);
                        relatedDto.setName(product.getName());
                        relatedDto.setImage(resourceHolder.getStringValue("imgRoot")+product.getDefaultImage());
                        result.add(relatedDto);
                        break;
                    }
                }
            }else if(SundayCommonConstants.activity_related_louKa.equals(related.getType())){
                for (SundayLouKa louKa:louKas) {
                    if(related.getOtherId().equals(louKa.getId())){
                        relatedDto.setType(SundayCommonConstants.activity_related_louKa);
                        relatedDto.setName(louKa.getName());
                        relatedDto.setImage(resourceHolder.getStringValue("imgRoot")+louKa.getDefaultImage());
                        result.add(relatedDto);
                        break;
                    }
                }
            }else if(SundayCommonConstants.activity_related_news.equals(related.getType())){
                for (SundayNews news:newss) {
                    if(related.getOtherId().equals(news.getId())){
                        relatedDto.setType(SundayCommonConstants.activity_related_news);
                        relatedDto.setName(news.getTitle());
                        relatedDto.setImage(resourceHolder.getStringValue("imgRoot")+news.getdefaultImage());
                        result.add(relatedDto);
                        break;
                    }
                }
            }

        }
        return  result;
    }

}
